<template>
  <BaseMainSlot :show-btn-box="true">
    <template v-slot:btn-box>
      <ActionBar
        :page-no="24"
        :add-active="true"
        :del-active="delActive"
        :has-cancel="false"
        @add="showDialog"
        @del="del"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="seach-box">
          <div class="select-box-content">
            <div class="select-box-content-name">产品名称：</div>
            <el-select
              v-model="querylist.materialNo"
              placeholder="请选择产品名称"
              filterable
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </div>
          <!-- <div class="select-box-content">
            <div class="select-box-content-name">产品名称：</div>
            <el-input
              v-model="querylist.materialName"
              placeholder="请输入产品名称"
              clearable
            />
          </div> -->
          <div class="select-box-content">
            <div class="select-box-content-name">产品编码：</div>
            <el-input
              v-model="querylist.materialNo"
              placeholder="请输入产品编码"
              clearable
            />
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <tdtable
        :page="page"
        :title="title"
        :prop-list="propList"
        :show-select-column="showSelectColumn"
        :show-index-column="showIndexColumn"
        :table-data="tableData"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        @handleSelectionChange="handleSelectionChange"
      >
        <!-- <template v-slot:handler="{row}">
          <div v-if="row.materialNo" class="pointer" @click="handleEdit(row)">
            <i class="el-icon-edit"></i>修改
          </div>
        </template> -->
        <template v-slot:pictureNo="{row}">
          <div
            v-if="row.materialNo"
            class="pointer"
            @click="showImgDialog(row.pictureNo)"
            style="cursor: pointer;"
          >
            <i class="el-icon-view"></i>查看图片
          </div>
        </template>
        <!-- <template v-slot:headerHandler>
          <el-button type="primary" class="botton" @click="showDialog">添加</el-button>
          <el-button type="primary" class="botton" @click="del">删除</el-button>
        </template>-->
      </tdtable>
    </template>
    <template v-slot:dialog>
      <AddDialog
        :show="show"
        :title-text="titleText"
        :options="options"
        :form-data-copy="formData"
        :unit-options="unitOptions"
        @closeDialog="closeDialog"
        @addEvent="addEvent"
      />
      <ImgDialog
        :picture-url="pictureUrl"
        :show="isShowImgDialog"
        @closeDialog="isShowImgDialog = false"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar/index1.vue";
import ImgDialog from "./components/img-dialog.vue";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import tdtable from "@/components/table/index.vue";
import AddDialog from "./components/add-pgpt-dialog.vue";
import SearchBox from "@/components/SearchBox/index.vue";
import productionApi from "@/api/processing-management/basic-info-manage/processing-product-manage/index";
// 获取物料分类的数据
import UnitApi from "@/api/processing-management/basic-info-manage/data-item-manage/index";
import QualityManage from "@/api/processing-management/quarity-manage";
import { mymixin } from "@/utils/materialMixin";
export default {
  name: "ProcessingProductManage",
  components: {
    BaseMainSlot,
    tdtable,
    AddDialog,
    SearchBox,
    ActionBar,
    ImgDialog
  },
  mixins: [mymixin],
  data() {
    return {
      unitOptions: [],
      handle: "",
      pictureUrl: "",
      isShowImgDialog: false,
      delActive: false,
      querylist: {
        materialTypeNo: "",
        materialName: "",
        materialNo: "",
        organizationNo: sessionStorage.organizationNo
      },
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      title: "加工产品管理列表",
      propList: [
        { prop: "materialTypeName", label: "所属产品类型", minWidth: "150" },
        {
          prop: "materialNo",
          label: "产品编码",
          minWidth: "100",
          slotName: "oldPrice"
        },
        { prop: "materialName", label: "产品名称", minWidth: "100" },
        {
          prop: "materialspec",
          label: "规格",
          minWidth: "100",
          slotName: "image"
        },
        { prop: "model", label: "型号", minWidth: "100", slotName: "status" },
        {
          prop: "unitName",
          label: "主单位",
          minWidth: "100",
          slotName: "status"
        },
        // {prop: 'createAt',label: '产品等级',minWidth: '250',slotName: 'createAt'},
        {
          prop: "pictureNo",
          label: "产品图片",
          minWidth: "150",
          slotName: "pictureNo"
        },
        // { label: "操作", minWidth: "120", slotName: "handler" }
      ],
      showIndexColumn: true,
      showSelectColumn: true,
      tableData: [],
      titleText: "产品管理",
      show: false,
      handleSelectList: [],
      formData: {}
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.queryPageList();
    this.getUnitOptions();
  },
  methods: {
    // 获取单位选择项
    getUnitOptions() {
      UnitApi.queryUnit().then(res => {
        if (res.code === 1) {
          this.unitOptions = res.data || [];
        }
      });
    },
    showImgDialog(pictureNo) {
      this.isShowImgDialog = true;
      if (pictureNo && pictureNo !== "#") {
        QualityManage.getPictureUrl(pictureNo).then(res => {
          this.pictureUrl = "data:image/png;base64," + res.data.data;
        });
      } else {
        this.pictureUrl = "";
      }
    },
    search() {
      this.queryPageList();
    },

    handleSizeChange(val) {
      this.page.pageSize = val;
      this.queryPageList();
    },

    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.queryPageList();
    },
    // 选中table行点击事件
    handleSelectionChange(val) {
      console.log(val);
      this.handleSelectList = [];
      val.forEach(e => {
        this.handleSelectList.push(e.materialNo);
      });
      if (this.handleSelectList.length > 0) {
        this.delActive = true;
      } else {
        this.delActive = false;
      }
    },
    queryPageList() {
      // 获取数据
      let param = {
        page: this.page.currentPage,
        size: this.page.pageSize,
        entity: this.querylist
      };
      productionApi.queryPageList(param).then(res => {
        if (res.code === 1) {
          let data = res.data;
          let result = [];
          this.page.total = data.totalElements;
          data.content.forEach(e => {
            result.push(e.material);
          });
          this.tableData = result;
        }
      });
    },
    // 添加物料
    addEvent(formData) {
      let params = formData;
      if (this.handle === "add") {
        productionApi.add(params).then(res => {
          console.log(res);
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
          }
        });
      }
      if (this.handle === "edit") {
        productionApi.edit(params).then(res => {
          console.log(res);
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
          }
        });
      }
    },
    // 删除供应商
    del() {
      let params = { noList: this.handleSelectList };
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          productionApi.del(params).then(res => {
            console.log(res);
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 修改数据
    handleEdit(row) {
      this.show = true;
      this.formData = { ...row };
      this.handle = "edit";
    },
    // 显示添加弹出框
    showDialog() {
      this.formData = {};
      this.handle = "add";
      this.show = true;
    },
    closeDialog() {
      this.show = false;
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.botton {
  background-color: #0091b7;
}
</style>
